package com.xsjiong.jaript.utils;

public final class JUtils {
	public static int BytesToInt(byte b1, byte b2, byte b3, byte b4) {
		return b1 & 0xFF |
				(b2 & 0xFF) << 8 |
				(b3 & 0xFF) << 16 |
				(b4 & 0xFF) << 24;
	}

	public static byte[] IntToBytes(int a) {
		return new byte[] {
				(byte) (a & 0xFF),
				(byte) ((a >> 8) & 0xFF),
				(byte) ((a >> 16) & 0xFF),
				(byte) ((a >> 24) & 0xFF)
		};
	}

	public static short BytesToShort(byte b1, byte b2) {
		return (short) (b1 & 0xFF | (b2 & 0xFF) << 8);
	}

	public static byte[] ShortToBytes(short a) {
		return new byte[] {
				(byte) (a & 0xFF),
				(byte) ((a >> 8) & 0xFF)
		};
	}

	public static float BytesToFloat(byte b1, byte b2, byte b3, byte b4) {
		return Float.intBitsToFloat(BytesToInt(b1, b2, b3, b4));
	}

	public static byte[] FloatToBytes(float a) {
		return IntToBytes(Float.floatToRawIntBits(a));
	}

	public static long BytesToLong(byte b1, byte b2, byte b3, byte b4, byte b5, byte b6, byte b7, byte b8) {
		return b1 & 0xFF |
				(b2 & 0xFF) << 8 |
				(b3 & 0xFF) << 16 |
				(b4 & 0xFF) << 24 |
				(b5 & 0xFF) << 32 |
				(b6 & 0xFF) << 40 |
				(b7 & 0xFF) << 48 |
				(b8 & 0xFF) << 56;
	}

	public static byte[] LongToBytes(long a) {
		return new byte[] {
				(byte) (a & 0xFF),
				(byte) ((a >> 8) & 0xFF),
				(byte) ((a >> 16) & 0xFF),
				(byte) ((a >> 24) & 0xFF),
				(byte) ((a >> 32) & 0xFF),
				(byte) ((a >> 40) & 0xFF),
				(byte) ((a >> 48) & 0xFF),
				(byte) ((a >> 56) & 0xFF)
		};
	}

	public static double BytesToDouble(byte b1, byte b2, byte b3, byte b4, byte b5, byte b6, byte b7, byte b8) {
		return Double.longBitsToDouble(BytesToLong(b1, b2, b3, b4, b5, b6, b7, b8));
	}

	public static byte[] DoubleToByts(double d) {
		return LongToBytes(Double.doubleToRawLongBits(d));
	}

	// Short 与 Char 容量相同，都是2Bytes
}
